const app = getApp();

//function getUserInfo(userInfo) {
function getUserInfo(avatarUrl,nickName) {
  console.log("参数avatarUrl：",avatarUrl);
  console.log("参数nickName：",nickName);
  let userInfo = {
    hasUserInfo: true,
    avatarUrl: avatarUrl,
    nickName: nickName
  }
  // 调用云函数获取微信的openid判断是不是管理员
  wx.cloud.callFunction({
    // 云函数名称
    name: 'getOpenID',
    // 传给云函数的参数
    data: {},
    success: (res) => {
      console.warn("你的OpenID为:", res.result.openid);
      userInfo.happyBean = res.result.happyBean
      userInfo.openid = res.result.openid
      if (res.result.openid == app.globalData.adminOpenid) {
        userInfo.Role = '管理员'
        userInfo.adminRoleFlag = true
      } else {
        userInfo.Role = '游客'
        userInfo.adminRoleFlag = false
      }
      console.log("最终要存入缓存的userInfo：",userInfo);
      app.globalData.userInfo = userInfo;
      wx.setStorageSync('userInfo', userInfo);
      // 有效期7天，1分钟=60000ms
      let expiration = Date.parse(new Date()) + (7 * 24 * 60 * 60000)
      wx.setStorage({
        data: expiration,
        key: 'userInfoExpiration'
      });
    },
    fail: (error) => {
      console.log("getOpenID云函数调用失败：", error);
      wx.showModal({
        title: '提示',
        content: 'getOpenID云函数调用失败'
      })
    }
  });

  console.log("返回的userInfo：",userInfo);
  return userInfo;
}

module.exports = {
  getUserInfo: getUserInfo
}
